package com.yixun.qfbao.api.service.impl;

import com.yixun.qfbao.api.service.DecisionConfigService;
import com.yixun.qfbao.apidto.*;
import com.yixun.qfbao.model.OverdueConfig;
import com.yixun.qfbao.model.dto.OverdueConfigDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.service.OverdueConfigService;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class DecisionConfigServiceImpl implements DecisionConfigService {
    @Autowired
    private OverdueConfigService overdueConfigService;

    /**
     * 根据楼盘获取逾期配置
     * @param reqDto
     */
    @Override
    public GetOverdueConfigInfoResDto getOverdueConfigInfo(GetOverdueConfigInfoReqDto reqDto){
        GetOverdueConfigInfoResDto resDto = new GetOverdueConfigInfoResDto();
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        if(reqDto.getPremisesId() == null){
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("楼盘信息不存在");
            return  resDto;
        }
        //获取该楼盘所有的逾期配置
        OverdueConfigDto overdueConfigDto = new OverdueConfigDto();
        overdueConfigDto.setPremisesId(reqDto.getPremisesId());
        overdueConfigDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
        overdueConfigDto.setDecisionService(reqDto.getDecisionService());
        List<OverdueConfig> overdueConfigList = overdueConfigService.selectListByDto(overdueConfigDto);
        List<OverdueConfig> overdueConfigSubNodes = new ArrayList<>();
        if(overdueConfigList != null && overdueConfigList.size() != 0){
            for(OverdueConfig oc : overdueConfigList){
                if(oc.getDecisionSort() == 0){
                    //总配置
                    resDto.setDecisionFit(oc.getDecisionApply());
                    resDto.setDecisionReset(oc.getDecisionReset());
                    if(oc.getUsable().equals(1) ){
                        //未开启逾期
                        resDto.setOverdueSet("1");
                    }else{
                        resDto.setOverdueSet("0");
                    }
                }else{
                    overdueConfigSubNodes.add(oc);
                }
            }
            resDto.setOverdueData(overdueConfigSubNodes);
        }
        return resDto;
    }


    /**
     * 新增或编辑楼盘逾期配置
     * @param reqDto
     * @return
     */
    @Override
    @Transactional
    public OverdueDecisionConfigResDto editOverdueDecisionConfig(OverdueDecisionConfigReqDto reqDto){
        OverdueDecisionConfigResDto resDto = new OverdueDecisionConfigResDto();
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        //统一更新统一新增,将原来的配置逻辑删除，再新增现有的配置
        if(reqDto.getPremisesId() == null){
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("楼盘信息不存在");
            return  resDto;
        }
        //1.更新该楼盘的逾期决策配置
        deleteOverdueConfigWithPremisesId(reqDto.getPremisesId(),reqDto.getDecisionService(),reqDto.getUserId());
        //2.重新添加新的逾期决策配置
        addOverdueConfig(reqDto);
        return  resDto;
    }

    /**
     * 新增楼盘时添加逾期决策配置
     * @param premisesId
     * @param userId
     * @param config
     */
    public void addOverdueDecisionConfig(Long premisesId,String userId,AddOverdueDecisionConfig config){
        if(premisesId == null){
            return ;
        }
        if(config == null){
            return;
        }
        //1.更新该楼盘的逾期决策配置(删除原有)
        deleteOverdueConfigWithPremisesId(config.getPremisesId(),config.getDecisionService(),userId);
        //2.添加逾期决策配置
        addOverdueConfig(config,userId,premisesId);
    }

    /**
     * 删除原有决策配置
     * @param premisesId
     * @param decisionService
     * @param userId
     */
    private void deleteOverdueConfigWithPremisesId(Long premisesId,Integer decisionService,String userId){
        if(premisesId == null ){
            return;
        }
        OverdueConfigDto overdueConfigDto = new OverdueConfigDto();
        overdueConfigDto.setPremisesId(premisesId);
        overdueConfigDto.setDelFlag(DelFlagEnum.DEL.getNo());
        overdueConfigDto.setDecisionService(decisionService);
        overdueConfigDto.setUpdateTime(new Date());
        if(StringUtils.isNotBlank(userId)){
            overdueConfigDto.setUpdateUserId(Long.parseLong(userId));
        }
        overdueConfigService.setOverdueConfigUsable(overdueConfigDto);
    }

    /**
     * 更新决策配置
     * @param reqDto
     */
    private void addOverdueConfig(OverdueDecisionConfigReqDto reqDto){
        int sort = 0;
        OverdueConfigDto overdueConfigDto = new OverdueConfigDto();
        overdueConfigDto.setPremisesId(reqDto.getPremisesId());
        overdueConfigDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
        overdueConfigDto.setDecisionService(reqDto.getDecisionService());
        if(StringUtils.isNotBlank(reqDto.getUserId())){
            overdueConfigDto.setUpdateUserId(Long.parseLong(reqDto.getUserId()));
        }
        // 1.新增总配置 sort为0 决策适用
        overdueConfigDto.setDecisionSort(sort);
        overdueConfigDto.setDecisionApply(reqDto.getDecisionApply());
        overdueConfigDto.setDecisionReset(reqDto.getResetNode());
        overdueConfigDto.setUsable(reqDto.getOverdueFlag());
        overdueConfigService.create(overdueConfigDto);
        overdueConfigDto.setDecisionReset(null);
        sort += 1;
        // 2.新增子逾期节点设置
        if(Objects.isNull(reqDto) || CollectionUtils.isEmpty(reqDto.getSubNodes())){
            return;
        }
        for(OverdueConfig oc : reqDto.getSubNodes()){
            overdueConfigDto.setId(null);
            overdueConfigDto.setDecisionSort(sort);
            overdueConfigDto.setDecisionType(oc.getDecisionType());
            overdueConfigDto.setDecisionTime(oc.getDecisionTime());
            overdueConfigDto.setDecisionTimeType(oc.getDecisionTimeType());
            overdueConfigDto.setDecisionAll(oc.getDecisionAll());
            overdueConfigService.create(overdueConfigDto);
            overdueConfigDto.setDecisionAll(null);
            sort += 1;
        }
    }

    /**
     * 添加楼盘时新增逾期决策配置
     * @param dto
     * @param userId
     */
    private void addOverdueConfig(AddOverdueDecisionConfig dto,String userId,Long premisesId){
        int sort = 0;
        OverdueConfigDto overdueConfigDto = new OverdueConfigDto();
        overdueConfigDto.setPremisesId(premisesId);
        overdueConfigDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
        overdueConfigDto.setUsable(dto.getOverdueFlag());

        overdueConfigDto.setDecisionService(dto.getDecisionService());
        if(StringUtils.isNotBlank(userId)){
            overdueConfigDto.setUpdateUserId(Long.parseLong(userId));
        }
        // 1.新增总配置 sort为0 决策适用
        overdueConfigDto.setDecisionSort(sort);
        overdueConfigDto.setDecisionApply(dto.getDecisionApply());
        overdueConfigDto.setDecisionReset(dto.getResetNode());

        overdueConfigService.create(overdueConfigDto);
        overdueConfigDto.setDecisionReset(null);
        sort += 1;
        // 2.新增子逾期节点设置
        if(dto.getSubNodes() == null && dto.getSubNodes().size() == 0){
            return;
        }
        for(OverdueConfig oc : dto.getSubNodes()){
            overdueConfigDto.setId(null);
            overdueConfigDto.setDecisionSort(sort);
            overdueConfigDto.setDecisionType(oc.getDecisionType());
            overdueConfigDto.setDecisionTime(oc.getDecisionTime());
            overdueConfigDto.setDecisionTimeType(oc.getDecisionTimeType());
            overdueConfigDto.setDecisionAll(oc.getDecisionAll());
            overdueConfigService.create(overdueConfigDto);
            overdueConfigDto.setDecisionAll(null);
            sort += 1;
        }
    }
    /**
     * 开启或关闭现有逾期
     * @param reqDto
     */
    private void setOverdueConfigDisabled(OverdueDecisionConfigReqDto reqDto){
        if(reqDto.getOverdueFlag() == 0){
            //未开启逾期决策
            OverdueConfigDto overdueConfigDto = new OverdueConfigDto();
            overdueConfigDto.setPremisesId(reqDto.getPremisesId());
            overdueConfigDto.setDecisionService(reqDto.getDecisionService());
            overdueConfigDto.setUsable(0);
            overdueConfigService.setOverdueConfigUsable(overdueConfigDto);
        }
    }
}
